home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / misc / bfd / binmod.c next >
Encoding:
C/C++ Source or Header  |  1999-06-17  |  4.5 KB  |  202 lines

  1. /*
  2.  * Copyright (C) Feb. 1999, Matt Conover & w00w00
  3.  *
  4.  * Demonstrates modifying opcodes in binary files. (Broken)
  5.  * To compile: gcc -o bfd bfd.c -lbfd -liberty
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include <errno.h>
  14. #include <ctype.h>
  15. #include <sys/types.h>
  16. #include <netinet/in.h>
  17. #include <bfd.h>
  18.  
  19. #if defined(SUN) || defined(SOLARIS)
  20. #include <inttypes.h>
  21. #endif
  22.  
  23. #define ERROR -1
  24. #define OFFSET  0x0 /* offset to preferred location */
  25.  
  26. #define CALL 0xe8 /* opcode for CALL on i386 */
  27. #define JMP 0xeb  /* opcode for JMP on i386  */
  28.  
  29. bfd_format bfdformat;
  30. enum bfd_flavour bfdflavour;
  31. bfd *abfd = NULL, *abfd1 = NULL;
  32.  
  33. u_long *entaddr = NULL;
  34. asection *asect = NULL;
  35.  
  36. int offset = OFFSET;
  37.  
  38. char ops[] = { (char)CALL, (char)JMP, '\0' };
  39.  
  40. /* --------------------------------------- */
  41.  
  42. /* ------- function declarations ------- */
  43. void setup();
  44. void showStats();
  45. void modSection(char *outfile, char *sectname);
  46.  
  47. void bfderror(char *fmt, ...);
  48. /* ------------------------------------- */
  49.  
  50. int main(int argc, char **argv)
  51. {   
  52.    if (argc <= 2)
  53.    {
  54.       fprintf(stderr, "Usage: %s <infile> <outfile> [addr offset]\n",
  55.               argv[0]);
  56.  
  57.       exit(ERROR); 
  58.    }
  59.  
  60.    else if (argc > 3) offset = atoi(argv[3]);
  61.  
  62.    bfd_init();
  63.    bfd_set_error_handler((bfd_error_handler_type)bfderror);
  64.  
  65.    abfd = bfd_openr(argv[1], NULL);
  66.  
  67.    setup(), showStats();
  68.    modSection(argv[2], ".text");
  69.  
  70.    bfd_close(abfd);
  71.    return 0;
  72. }
  73.  
  74.  
  75. void setup()
  76. {
  77.    bfdformat = bfd_object;
  78.    if (bfd_check_format(abfd, bfdformat) == false)
  79.    {
  80.       (void)fprintf(stderr, "error: only object files are supported\n\n");
  81.       exit(ERROR);
  82.    }
  83.  
  84.    bfdflavour = bfd_get_flavour(abfd);
  85.    if (bfdflavour == ERROR)
  86.    {
  87.       bfderror("[bfd_get_flavour] error");
  88.       exit(ERROR);
  89.    }
  90.  
  91.    /* ---------------------------------------------------- */
  92.  
  93.    if ((bfdflavour == bfd_target_unknown_flavour) ||
  94.        ((bfdflavour != bfd_target_aout_flavour) &&
  95.         (bfdflavour != bfd_target_coff_flavour) &&
  96.         (bfdflavour != bfd_target_elf_flavour)))
  97.    {
  98.       (void)fprintf(stderr, "Only current supported formats (flavours): "
  99.                             "ELF, COFF, and AOUT\n");
  100.  
  101.       exit(ERROR);
  102.    }
  103. }
  104.  
  105.  
  106. void showStats()
  107. {   
  108.    (void)printf("Filename: %s\n\n", bfd_get_filename(abfd));
  109.    (void)printf("File's target: %s\n", bfd_get_target(abfd));
  110.  
  111.    entaddr = (u_long *)bfd_get_start_address(abfd);
  112.    (void)printf("Start address: %p\n\n", entaddr);
  113. }
  114.  
  115.  
  116. void modSection(char *outfile, char *sectname)
  117. {
  118.    register int i;
  119.    int sectmod = 0, sectsize = 0;
  120.    char *sectbuf, *sectptr = NULL;
  121.  
  122.    asect = bfd_get_section_by_name(abfd, sectname);
  123.    if (asect == NULL) 
  124.    {
  125.       bfderror("[bfd_get_section_by_name] error opening %s\n", sectname);
  126.       exit(ERROR);
  127.    }
  128.  
  129.    sectsize = bfd_get_section_size_before_reloc(asect);
  130.  
  131.    sectbuf = (char *)malloc(sectsize+1);
  132.    memset(sectbuf, 0, sectsize+1);
  133.  
  134.    if (bfd_get_section_contents(abfd, asect, sectbuf, 0, sectsize) == false)
  135.    {
  136.       bfderror("[bfd_get_section_contents] error opening %s section", 
  137.                sectname);
  138.  
  139.       exit(ERROR);
  140.    }
  141.  
  142.    printf("%s size = 0x%x (%d) bytes\n\n", sectname, sectsize, sectsize);
  143.    printf("Start (%p) - End (%p)\n", sectbuf, §buf[sectsize]);
  144.    sleep(2);
  145.  
  146.    for (sectptr = sectbuf; sectptr < §buf[sectsize]; sectptr++)
  147.    {
  148.       for (i = 0; ops[i]; i++)
  149.          if (*sectptr == ops[i])
  150.          {
  151.             printf("0x%02x found at [%s] + 0x%x\n",
  152.                    ops[i], sectname, (u_long)sectptr - (u_long)sectbuf, 
  153.                    (u_long)sectptr - (u_long)sectbuf);
  154.  
  155.             sectptr += 1; /* 4-byte address after jmp/call opcode */
  156.  
  157.             /* ----------------------------------- */
  158.  
  159. /*
  160.             if (!bfd_little_endian(abfd))
  161.                *((u_long *)sectptr) = (u_long)(entaddr + offset);
  162.  
  163.             else *((u_long *)sectptr) = ntohl((u_long)(entaddr + offset));
  164. */
  165.  
  166.             sectmod = 1;
  167.          }
  168.    }
  169.  
  170.    if (sectmod)
  171.    {
  172.       abfd1 = bfd_openw(outfile, NULL);
  173.  
  174.       if (abfd1 == NULL)
  175.       {
  176.          bfderror("[bfd_openw] error with opening %s", outfile);
  177.          exit(ERROR);
  178.       }
  179.  
  180.       bfd_copy_private_bfd_data(abfd, abfd1);
  181.       bfd_set_section_contents(abfd1, asect, sectbuf, 0, sectsize);
  182.  
  183.       bfd_close(abfd1);
  184.    }
  185. }
  186.  
  187.  
  188. /* our error message handler */
  189. void bfderror(char *fmt, ...)
  190. {
  191.    va_list va;
  192.    char errbuf[512] = {0};
  193.  
  194.    va_start(va, fmt);
  195.    vsnprintf(errbuf, sizeof(errbuf)-1, fmt, va);
  196.    va_end(va);
  197.  
  198.    bfd_perror(errbuf);
  199. }
  200.  
  201.  
  202.